package com.example.photography.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.fastjson.JSONObject;
import com.example.photography.R;
import com.example.photography.bean.FocusBean;
import com.example.photography.bean.Order_photoBean;
import com.example.photography.common.BaseActivity;
import com.example.photography.fragment.order.Order_model;
import com.example.photography.fragment.order.Order_photographer;
import com.example.photography.util.UIUtils;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.example.photography.Activity.Store_Activity.getJson;

public class Order_photo extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tb_item)
    TabLayout tbItem;
    @BindView(R.id.vp_invest)
    ViewPager vpInvest;
    private List<Fragment> fragmentList = new ArrayList<>();
    private FocusBean focusBean;
    private Order_photoBean orderPhotoBean;

    @Override
    protected void initData() {

        String json = getJson(this, "order_photo.json");
        if (!json.isEmpty()) {
            orderPhotoBean = JSONObject.parseObject(json, Order_photoBean.class);
        }
        initView();
    }

    private void initView() {
        tvTitle.setText("约拍订单");
        initFragments();
        initListen();
        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager());
        vpInvest.setAdapter(myAdapter);
        vpInvest.setCurrentItem(0);//设置viewpager的初始化位置
        tbItem.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //将TabLayout绑定ViewPager
        tbItem.setupWithViewPager(vpInvest);
        //TabLayout平分空间
        tbItem.setTabMode(TabLayout.MODE_FIXED);
        tbItem.setTabGravity(TabLayout.GRAVITY_FILL);
    }

    private void initListen() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                removeCurrentActivity();
            }
        });

    }

    private void initFragments() {
        //添加到集合中
        fragmentList.add(new Order_model(orderPhotoBean.getData().getModel()));
        fragmentList.add(new Order_photographer(orderPhotoBean.getData().getPhotographer()));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_photo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 提供PagerAdapter的实现
     * 如果ViewPager中加载的是Fragment,则提供的Adapter可以继承于具体的：FragmentStatePagerAdapter或FragmentPagerAdapter
     * FragmentStatePagerAdapter:适用于ViewPager中加载的Fragment过多，会根据最近最少使用算法，实现内存中Fragment的清理，避免溢出
     * FragmentPagerAdapter:适合少量的页面显示。该类每一个生成的Fragment对象都会储存在内存中，所以适合相对静态、页面少的情况，如果是页面多，且Fragment的处理相对动态（添加、删除等）时，使用FragmentStatePagerAdapter较为适合。
     */
    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return fragmentList == null ? 0 : fragmentList.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            /**
             * 因为一般数组一旦初始化成功就无法进行增删操作
             * 所以我们要将其转List数组
             */
            List<String> home_tab = new ArrayList<String>(Arrays.asList(UIUtils.getStringArr(R.array.appointment_tab)));

            //todo 放大字体显示不全
            return home_tab.get(position);
        }
    }
}
